package com.shenmazong.grade.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.shenmazong.grade.entity.TbTaskGrade;
import com.shenmazong.grade.entity.TbTaskSkillGradeDetail;
import com.shenmazong.grade.entity.TbTaskTheoryGradeDetail;
import com.shenmazong.grade.mapper.TbTaskSkillGradeDetailMapper;
import com.shenmazong.grade.mapper.TbTaskTheoryGradeDetailMapper;
import com.shenmazong.grade.service.TaskGradeService;
import com.shenmazong.grade.mapper.TbTaskGradeMapper;
import com.shenmazong.manage.entity.TbTaskSubmit;
import com.shenmazong.manage.mapper.TbTaskSubmitMapper;
import com.shenmazong.util.ChooseIsNull;
import com.shenmazong.util.MyTimeUtil;
import com.shenmazong.utils.ResultResponse;
import com.shenmazong.vo.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import static com.shenmazong.config.MqConfig.TASK_SUBMIT_QUEUE;

/**
 *serviceImpl Qi
 */
@Service
public class TaskGradeServiceImpl extends ServiceImpl<TbTaskGradeMapper, TbTaskGrade>
    implements TaskGradeService {

    @Autowired
    private  TbTaskGradeMapper  gradeMapper;
    @Autowired
    private TbTaskTheoryGradeDetailMapper theoryGDMapper;
    @Autowired
    private TbTaskSkillGradeDetailMapper skillGDMapper;
    @Autowired
    private TbTaskSubmitMapper submitMapper;
    @Autowired
    private RabbitTemplate mqTemplate;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public ResultResponse saveTaskGrade(TbTaskGrade tbTaskGrade) {
        int i = gradeMapper.insert(tbTaskGrade);
        if(i<1){
            return ResultResponse.FAILED("新增失败");
        }
        return ResultResponse.SUCCESS(gradeMapper.selectById(tbTaskGrade.getGradeId()));
    }

    @Override
    public ResultResponse deleteTaskGrade(BatchChooseIdVo chooseIdVo) {
        if (ChooseIsNull.idsIsNull(chooseIdVo)){
            return ResultResponse.FAILED("未选择要删除的数据");
        }
        int i = gradeMapper.deleteBatchIds(chooseIdVo.getIds());
        if(i<1){
            return ResultResponse.FAILED("删除失败");
        }
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse updateTaskGrade(TbTaskGrade tbTaskGrade) {
        int i = gradeMapper.updateById(tbTaskGrade);
        if(i<1){
            return ResultResponse.FAILED("修改失败");
        }
        return ResultResponse.SUCCESS(gradeMapper.selectById(tbTaskGrade.getGradeId()));
    }

    @Override
    public ResultResponse listTaskGrade(ListParamVo paramVo) {
        //非空判断
        if(paramVo.getParamMap().size()==0 || paramVo.getParamMap().get("schoolId")==null){
            return ResultResponse.FAILED("无效查询条件");
        }

        //学生姓名 课程ID 班级ID 教师ID 空处理
        if(StringUtils.checkValNull(paramVo.getParamMap().get("studentName"))){
            paramVo.getParamMap().put("studentName","");
        }
        if(StringUtils.checkValNull(paramVo.getParamMap().get("courseId"))){
            paramVo.getParamMap().put("courseId","");
        }
        if(StringUtils.checkValNull(paramVo.getParamMap().get("clazzId"))){
            paramVo.getParamMap().put("clazzId","");
        }
        if(StringUtils.checkValNull(paramVo.getParamMap().get("teacherId"))){
            paramVo.getParamMap().put("teacherId","");
        }
        //区间查询空处理

        //查询数据
        IPage<TaskGradeVo> page = new Page<>(paramVo.getCurrent(), paramVo.getSize());
        IPage<TaskGradeVo> selectPage = gradeMapper.selectTaskGradeList(page,paramVo.getParamMap());
        return ResultResponse.SUCCESS(selectPage);
    }

    @Override
    public ResultResponse getTaskGrade(TbTaskGrade tbTaskGrade) {
        if(StringUtils.checkValNull(tbTaskGrade.getGradeId())){
            return ResultResponse.FAILED("ID为空");
        }
        return ResultResponse.SUCCESS(gradeMapper.selectById(tbTaskGrade.getGradeId()));
    }

    @Override
    public ResultResponse saveTaskTheoryGradeDetail(TbTaskTheoryGradeDetail tbTaskTheoryGradeDetail) {
        int i = theoryGDMapper.insert(tbTaskTheoryGradeDetail);
        if(i<1){
            return ResultResponse.FAILED("新增失败");
        }
        return ResultResponse.SUCCESS(theoryGDMapper.selectById(tbTaskTheoryGradeDetail.getTheoryDetailId()));
    }

    @Override
    public ResultResponse deleteTaskTheoryGradeDetail(BatchChooseIdVo chooseIdVo) {
        if (ChooseIsNull.idsIsNull(chooseIdVo)){
            return ResultResponse.FAILED("未选择要删除的数据");
        }
        int i = theoryGDMapper.deleteBatchIds(chooseIdVo.getIds());
        if(i<1){
            return ResultResponse.FAILED("删除失败");
        }
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse updateTaskTheoryGradeDetail(TbTaskTheoryGradeDetail tbTaskTheoryGradeDetail) {
        int i = theoryGDMapper.updateById(tbTaskTheoryGradeDetail);
        if(i<1){
            return ResultResponse.FAILED("修改失败");
        }
        return ResultResponse.SUCCESS(theoryGDMapper.selectById(tbTaskTheoryGradeDetail.getTheoryDetailId()));
    }

    @Override
    public ResultResponse listTaskTheoryGradeDetail(ListParamVo paramVo) {
        //非空判断
        if(paramVo.getParamMap().size()==0 || paramVo.getParamMap().get("gradeId")==null){
            return ResultResponse.FAILED("无效查询条件");
        }

        //题目类型 空处理
        if(StringUtils.checkValNull(paramVo.getParamMap().get("theoryType"))){
            paramVo.getParamMap().put("theoryType","");
        }

        //查询数据
        IPage<TaskTheoryGradeDetailVo> page = new Page<>(paramVo.getCurrent(), paramVo.getSize());
        IPage<TaskTheoryGradeDetailVo> selectPage = theoryGDMapper.selectTheoryGradeDetailList(page,paramVo.getParamMap());
        return ResultResponse.SUCCESS(selectPage);
    }

    @Override
    public ResultResponse saveTaskSkillGradeDetail(TbTaskSkillGradeDetail tbTaskSkillGradeDetail) {
        int i = skillGDMapper.insert(tbTaskSkillGradeDetail);
        if(i<1){
            return ResultResponse.FAILED("新增失败");
        }
        return ResultResponse.SUCCESS(skillGDMapper.selectById(tbTaskSkillGradeDetail.getSkillDetailId()));
    }

    @Override
    public ResultResponse deleteTaskSkillGradeDetail(BatchChooseIdVo chooseIdVo) {
        if (ChooseIsNull.idsIsNull(chooseIdVo)){
            return ResultResponse.FAILED("未选择要删除的数据");
        }
        int i = skillGDMapper.deleteBatchIds(chooseIdVo.getIds());
        if(i<1){
            return ResultResponse.FAILED("删除失败");
        }
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse updateTaskSkillGradeDetail(TbTaskSkillGradeDetail tbTaskSkillGradeDetail) {
        int i = skillGDMapper.updateById(tbTaskSkillGradeDetail);
        if(i<1){
            return ResultResponse.FAILED("修改失败");
        }
        return ResultResponse.SUCCESS(skillGDMapper.selectById(tbTaskSkillGradeDetail.getSkillDetailId()));
    }

    @Override
    public ResultResponse listTaskSkillGradeDetail(ListParamVo paramVo) {
        //非空判断
        if(paramVo.getParamMap().size()==0 || paramVo.getParamMap().get("gradeId")==null){
            return ResultResponse.FAILED("无效查询条件");
        }

        //查询数据
        IPage<TaskSkillGradeDetailVo> page = new Page<>(paramVo.getCurrent(), paramVo.getSize());
        IPage<TaskSkillGradeDetailVo> selectPage = skillGDMapper.selectTheorySkillDetailList(page,paramVo.getParamMap());
        return ResultResponse.SUCCESS(selectPage);
    }

    @Override
    public ResultResponse taskTheoryAutoMake(TaskStudentSubmitVo submitVo) {

        //判断是否重复提交
        String submitKey = "task_submit_set_"+submitVo.getLayoutId();
        if(redisTemplate.opsForSet().isMember(submitKey,String.valueOf(submitVo.getStudentId()))){
            return ResultResponse.FAILED("请勿重复提交");
        }
            ObjectMapper mapper = new ObjectMapper();
        try {
            //内容发送到mq队列
            String value = mapper.writeValueAsString(submitVo);
            mqTemplate.convertAndSend(TASK_SUBMIT_QUEUE,value);

        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.FAILED("提交失败");
        }

        //Redis提交集合中加入该学生ID
        redisTemplate.opsForSet().add(submitKey,String.valueOf(submitVo.getStudentId()));
        return ResultResponse.SUCCESS("提交成功");
    }
}




